package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityGoodsInfo;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityChartValue;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityInOut;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * CREATE BY zlzxm ON 4/21/21
 */
public class VMMarketDynamic extends ViewModel {


    public static final int PRICE_TODAY = 1;
    public static final int PRICE_SEVEN_DAY = 2;
    public static final int PRICE_MONTH = 3;


    public static final int INOUT_SEVEN_DAY = 1;
    public static final int INOUT_MONTH = 2;
    public static final int INOUT_YEAR = 3;

    private MutableLiveData<List<EntityChartValue>> livePriceInfo = new MutableLiveData<>();

    private MutableLiveData<EntityInOut> liveInOutInfo = new MutableLiveData<>();

    public MutableLiveData<List<EntityChartValue>> getLivePriceInfo() {
        return livePriceInfo;
    }

    public MutableLiveData<EntityInOut> getLiveInOutInfo() {
        return liveInOutInfo;
    }

    public void getPriceData(int by){

        Observable.timer(1, TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                        livePriceInfo.setValue(createTestPriceData(by));
                    }
                });




    }


    public void getInOutData(int by){

        Observable.timer(1, TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                        liveInOutInfo.setValue(createTestInOutData(by));
                    }
                });




    }




    //<editor-fold defaultState="collapsed" desc="测试">

    private List<EntityGoodsInfo> createTestInfo(){

        List<EntityGoodsInfo> list = new ArrayList<>();

        list.add(new EntityGoodsInfo("货         号","xxx-xx"));
        list.add(new EntityGoodsInfo("云仓总库存","29283"));
        list.add(new EntityGoodsInfo("平台总销量","29283"));
        list.add(new EntityGoodsInfo("三日均销量","283"));

        return list;

    }


    private List<EntityChartValue> createTestPriceData(int by){


        List<EntityChartValue> list = new ArrayList<>();


        if(by == PRICE_TODAY){

            list.add(new EntityChartValue("7:00",18));
            list.add(new EntityChartValue("10:00",40));
            list.add(new EntityChartValue("13:00",28));
            list.add(new EntityChartValue("16:00",42));
            list.add(new EntityChartValue("19:00",58));
            list.add(new EntityChartValue("22:00",89));
            list.add(new EntityChartValue("24:00",80));

        }else  if(by == PRICE_SEVEN_DAY){

            list.add(new EntityChartValue("27号",2));
            list.add(new EntityChartValue("28号",20));
            list.add(new EntityChartValue("29号",80));
            list.add(new EntityChartValue("30号",89));
            list.add(new EntityChartValue("31号",70));
            list.add(new EntityChartValue("1号",50));
            list.add(new EntityChartValue("2号",40));

        }else  if(by == PRICE_MONTH){

            for (int i = 0; i < 30; i++) {

                Random random = new Random();

                list.add(new EntityChartValue(i+1+"号",random.nextInt(100)));

            }

        }




        return  list;

    }

    private EntityInOut createTestInOutData(int by){


        List<EntityChartValue> listIn = new ArrayList<>();

        List<EntityChartValue> listOut = new ArrayList<>();


        if(by == INOUT_SEVEN_DAY){

            listIn.add(new EntityChartValue("7:00",18));
            listIn.add(new EntityChartValue("10:00",40));
            listIn.add(new EntityChartValue("13:00",28));
            listIn.add(new EntityChartValue("16:00",42));
            listIn.add(new EntityChartValue("19:00",58));
            listIn.add(new EntityChartValue("22:00",89));
            listIn.add(new EntityChartValue("24:00",80));


            listOut.add(new EntityChartValue("27",0));
            listOut.add(new EntityChartValue("28",21));
            listOut.add(new EntityChartValue("29",8));
            listOut.add(new EntityChartValue("30",31));
            listOut.add(new EntityChartValue("31",20));
            listOut.add(new EntityChartValue("32",50));
            listOut.add(new EntityChartValue("33",100));



        }else  if(by == INOUT_MONTH){

            for (int i = 0; i < 30; i++) {

                Random random = new Random();

                listIn.add(new EntityChartValue(i+1+"号",random.nextInt(100)));

                listOut.add(new EntityChartValue(i+1+"号",random.nextInt(100)));

            }

        }else  if(by == INOUT_YEAR){

            for (int i = 0; i < 30; i++) {

                Random random = new Random();

                listIn.add(new EntityChartValue(i+1+"号",random.nextInt(100)));

                listOut.add(new EntityChartValue(i+1+"号",random.nextInt(100)));

            }

        }


        return  new EntityInOut(listIn,listOut);

    }


// </editor-fold>
}
